package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import com.it2297.controller.CompanyController;
import com.it2297.controller.JobController;
import com.it2297.main.App;
import com.it2297.model.Job;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.awt.Color;
import javax.swing.JCheckBox;

public class JobSearchJob extends JPanel {
	private JTextField textField;
	private JComboBox comboBox;
	private JLabel lblNewLabel_1;
	private JLabel lbl1;
	private JLabel lbl2;
	private JLabel lbl3;
	private JLabel lbl4;
	private JLabel lbl5;
	private JLabel lbl7;
	private JLabel lbl8;
	private JLabel lbl9;
	private JLabel lbl10;
	private JTextField tf_desc;
	private JTextField tf_req;
	private JTextField tf_duty;
	private JTextField tf_it;
	private JTextField tf_et;
	private JTextField tf_wst;
	private JTextField tf_wet;
	private JTextField tf_salary;
	private JTextField tf_benefit;
	private JTextField tf_cname;
	private JButton btnViewCompany;
	private JButton btnApply;
	private JButton btnNewButton_1;
	private JLabel lblCurrent;
	private JLabel lblTo;
	private JCheckBox cb_title;
	private JCheckBox cb_requirement;
	private JLabel lblcriteria; 
	private JCheckBox cb_duty;
	private JLabel lblNewLabel_2;
	final JobController jct= new JobController(this);
	private String cname;
	private String title;
	private JTextField tf_jobid;
	private JLabel lbljobid;
	//final CompanyControllerTest cct = new CompanyControllerTest(this);
	
	public void setTitle(String title){
		this.title=title;
	}
	public void setDescription(String desc){
		tf_desc.setText(desc);
	}
	public void setRequirement(String req){
		tf_req.setText(req);
	}
	public void setDuty(String duty){
		tf_duty.setText(duty);
	}
	public void setIndustryType(String it){
		tf_it.setText(it);
	}
	public void setEmploymentType(String et){
		tf_et.setText(et);
	}
	public void setStartTime(String wst){
		tf_wst.setText(wst);
	}
	public void setEndTime(String wet){
		tf_wet.setText(wet);
	}
	public void setSalary(String salary){
		tf_salary.setText(salary);
	}
	public void setBenefit(String benefit){
		tf_benefit.setText(benefit);
	}
	public void setCompanyName(String cname){
		tf_cname.setText(cname);
		this.cname=cname;
	}
	public void setJobID(String jobid){
		tf_jobid.setText(jobid);
	}
	public String getDescription(){
		return tf_desc.getText();
	}
	public String getRequirement(){
		return tf_req.getText();
	}
	public String getDuty(){
		return tf_duty.getText();
	}
	public String getIndustryType(){
		return tf_it.getText();
	}
	public String getEmployementType(){
		return tf_et.getText();
	}
	public String getStartTime(){
		return tf_wst.getText();
	}
	public String getEndTime(){
		return tf_wet.getText();
	}
	public String getSalary(){
		return tf_salary.getText();
	}
	public String getBenefit(){
		return tf_desc.getText();
	}
	public String getCompanyName(){
		return tf_cname.getText();
	}
	public String getSearch(){
		return textField.getText();
	}
	public String getTitle(){
		return comboBox.getSelectedItem().toString();
	}
	
	public JobSearchJob() {
		setLayout(null);

		JLabel lblNewLabel = new JLabel("Job Search");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
		lblNewLabel.setBounds(10, 0, 720, 54);
		add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(154, 65, 416, 23);
		add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Search");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(textField.getText().equals("")){
					comboBox.removeAllItems();
					comboBox.setVisible(false);
					comboBox.revalidate();
					
					hideTextFields();
					JOptionPane.showMessageDialog(null,"Please enter a search.");
					
				}
				else{
							comboBox.removeAllItems();
							comboBox.setVisible(false);
							comboBox.revalidate();
							hideTextFields();
		
							 for(int i=0; i<jct.searchForMatch().size();i++){
					                comboBox.addItem(jct.searchForMatch().get(i));      
							 }
							
							if(title==""){
								JOptionPane.showMessageDialog(null, "No job(s) matches the search: \"" + textField.getText().toString() + "\", please try something else.");
								comboBox.removeAllItems();
								comboBox.setVisible(false);
								comboBox.revalidate();
								textField.setText("");
							}
							else{
								comboBox.setVisible(true);
								comboBox.addItem(title);
								lblNewLabel_1.setVisible(true);
								int amt =comboBox.getItemCount()-1;
								lblNewLabel_1.setText(amt + " job(s) found.");
								btnNewButton_1.setVisible(true);
								textField.setText("");
							}
					}
			}
		});
		btnNewButton.setFont(new Font("Dialog", Font.BOLD, 18));
		btnNewButton.setBounds(580, 65, 111, 23);
		add(btnNewButton);
		
		lblcriteria = new JLabel("Enter job title:");
		lblcriteria.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblcriteria.setBounds(41, 65, 141, 23);
		add(lblcriteria);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 if(comboBox.getSelectedItem()==null){
						
						lbl1.setVisible(false);
						lbl2.setVisible(false);
						lbl3.setVisible(false);
						lbl4.setVisible(false);
						lbl5.setVisible(false);
						lbl7.setVisible(false);
						lbl8.setVisible(false);
						lbl9.setVisible(false);
						lbl10.setVisible(false);
						lbljobid.setVisible(false);
						tf_jobid.setVisible(false);
						tf_desc.setVisible(false);
						tf_req.setVisible(false);
						tf_duty.setVisible(false);
						tf_it.setVisible(false);
						tf_et.setVisible(false);
						tf_wst.setVisible(false);
						tf_wet.setVisible(false);
						tf_salary.setVisible(false);
						tf_benefit.setVisible(false);
						tf_cname.setVisible(false);
						btnViewCompany.setVisible(false);
						btnApply.setVisible(false);
						//btnNewButton_1.setVisible(false);
						lblCurrent.setVisible(false);
						lblTo.setVisible(false);
						
						
						comboBox.removeItem(comboBox.getSelectedItem());
						//JOptionPane.showMessageDialog(null, "Invalid operation! Please view another job.");
						//comboBox.setSelectedIndex(0);
						
						
					}
			}	
		});
		comboBox.setVisible(false);
		comboBox.setBounds(177, 99, 364, 23);
		add(comboBox);
		
		lblNewLabel_1 = new JLabel("0 job(s) found");
		lblNewLabel_1.setForeground(new Color(128, 0, 0));
		lblNewLabel_1.setVisible(false);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel_1.setBounds(41, 92, 131, 32);
		add(lblNewLabel_1);
		
		tf_desc = new JTextField();
		tf_desc.setEditable(false);
		tf_desc.setVisible(false);
		tf_desc.setBounds(262, 233, 457, 23);
		add(tf_desc);
		tf_desc.setColumns(10);
		
		lbl1 = new JLabel("Job Description:");
		lbl1.setVisible(false);
		lbl1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl1.setBounds(112, 231, 140, 23);
		add(lbl1);
		
		tf_req = new JTextField();
		tf_req.setEditable(false);
		tf_req.setVisible(false);
		tf_req.setBounds(262, 265, 457, 23);
		add(tf_req);
		tf_req.setColumns(10);
		
		lbl2 = new JLabel("Job Requirement:");
		lbl2.setVisible(false);
		lbl2.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl2.setBounds(112, 265, 140, 19);
		add(lbl2);
		
		tf_duty = new JTextField();
		tf_duty.setEditable(false);
		tf_duty.setVisible(false);
		tf_duty.setBounds(262, 299, 457, 23);
		add(tf_duty);
		tf_duty.setColumns(10);
		
		lbl3 = new JLabel("Duty:");
		lbl3.setVisible(false);
		lbl3.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl3.setBounds(112, 299, 111, 19);
		add(lbl3);
		
		tf_it = new JTextField();
		tf_it.setEditable(false);
		tf_it.setVisible(false);
		tf_it.setBounds(262, 333, 267, 23);
		add(tf_it);
		tf_it.setColumns(10);
		
		lbl4 = new JLabel("Industry Type:");
		lbl4.setVisible(false);
		lbl4.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl4.setBounds(112, 331, 111, 23);
		add(lbl4);
		
		tf_et = new JTextField();
		tf_et.setEditable(false);
		tf_et.setVisible(false);
		tf_et.setBounds(262, 367, 140, 23);
		add(tf_et);
		tf_et.setColumns(10);
		
		lbl5 = new JLabel("Employment Type:");
		lbl5.setVisible(false);
		lbl5.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl5.setBounds(112, 365, 140, 23);
		add(lbl5);
		
		tf_wst = new JTextField();
		tf_wst.setEditable(false);
		tf_wst.setVisible(false);
		tf_wst.setBounds(262, 401, 125, 22);
		add(tf_wst);
		tf_wst.setColumns(10);
		
		lbl7 = new JLabel("Working Time:");
		lbl7.setVisible(false);
		lbl7.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl7.setBounds(112, 401, 153, 23);
		add(lbl7);
		
		tf_wet = new JTextField();
		tf_wet.setEditable(false);
		tf_wet.setVisible(false);
		tf_wet.setBounds(425, 401, 125, 22);
		add(tf_wet);
		tf_wet.setColumns(10);
		
		lbl8 = new JLabel("Salary(SGD($)):");
		lbl8.setVisible(false);
		lbl8.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl8.setBounds(112, 435, 111, 23);
		add(lbl8);
		
		tf_salary = new JTextField();
		tf_salary.setEditable(false);
		tf_salary.setVisible(false);
		tf_salary.setBounds(262, 434, 104, 23);
		add(tf_salary);
		tf_salary.setColumns(10);
		
		lbl9 = new JLabel("Benefit:");
		lbl9.setVisible(false);
		lbl9.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl9.setBounds(112, 469, 111, 23);
		add(lbl9);
		
		tf_benefit = new JTextField();
		tf_benefit.setEditable(false);
		tf_benefit.setVisible(false);
		tf_benefit.setBounds(262, 468, 457, 23);
		add(tf_benefit);
		tf_benefit.setColumns(10);
		
		lbl10 = new JLabel("Company Name:");
		lbl10.setVisible(false);
		lbl10.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl10.setBounds(112, 198, 140, 23);
		add(lbl10);
		
		tf_cname = new JTextField();
		tf_cname.setEditable(false);
		tf_cname.setVisible(false);
		tf_cname.setBounds(262, 200, 282, 23);
		add(tf_cname);
		tf_cname.setColumns(10);
		
		btnViewCompany = new JButton("View Company");
		btnViewCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyViewCompanyDetails vcdt=new CompanyViewCompanyDetails();
				vcdt.setCompanyName(tf_cname.getText());
				vcdt.setLocationRelativeTo(null);
				vcdt.setVisible(true);
			}
		});
		btnViewCompany.setVisible(false);
		btnViewCompany.setFont(new Font("Dialog", Font.BOLD, 18));
		btnViewCompany.setBounds(262, 502, 167, 23);
		add(btnViewCompany);
		
		btnApply = new JButton("Apply for job");
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//JOptionPane.showMessageDialog(null, "Not done yet!!");
				JobApplication jat=new JobApplication();
				jat.setJobTitle(comboBox.getSelectedItem().toString());
				jat.setLocationRelativeTo(null);
				jat.setVisible(true);
				
			}
		});
		btnApply.setVisible(false);
		btnApply.setFont(new Font("Dialog", Font.BOLD, 18));
		btnApply.setBounds(439, 502, 167, 23);
		add(btnApply);
		
		btnNewButton_1 = new JButton("View Job Details");
		btnNewButton_1.setFont(new Font("Californian FB", Font.BOLD, 14));
		btnNewButton_1.setVisible(false);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						jct.retrieveFullJobDetails();					
						showTextFields();
						lblCurrent.setText("Currently viewing job: " + comboBox.getSelectedItem());
			}
		});
		btnNewButton_1.setBounds(551, 99, 140, 23);
		add(btnNewButton_1);
		
		lblCurrent = new JLabel("lbl");
		lblCurrent.setForeground (Color.blue);
		lblCurrent.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 14));
		lblCurrent.setVisible(false);
		lblCurrent.setBounds(92, 133, 603, 20);
		add(lblCurrent);
		
		lblTo = new JLabel("To");
		lblTo.setVisible(false);
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblTo.setHorizontalAlignment(SwingConstants.CENTER);
		lblTo.setBounds(371, 399, 72, 23);
		add(lblTo);
		
		lbljobid = new JLabel("Job ID:");
		lbljobid.setVisible(false);
		lbljobid.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbljobid.setBounds(112, 164, 140, 23);
		add(lbljobid);
		
		tf_jobid = new JTextField();
		tf_jobid.setVisible(false);
		tf_jobid.setEditable(false);
		tf_jobid.setBounds(262, 166, 45, 23);
		add(tf_jobid);
		tf_jobid.setColumns(10);
	}
	
	public void hideTextFields(){
		lblNewLabel_1.setVisible(false);
		
		lbl1.setVisible(false);
		lbl2.setVisible(false);
		lbl3.setVisible(false);
		lbl4.setVisible(false);
		lbl5.setVisible(false);
		lbl7.setVisible(false);
		lbl8.setVisible(false);
		lbl9.setVisible(false);
		lbl10.setVisible(false);
		lbljobid.setVisible(false);
		tf_jobid.setVisible(false);
		tf_desc.setVisible(false);
		tf_req.setVisible(false);
		tf_duty.setVisible(false);
		tf_it.setVisible(false);
		tf_et.setVisible(false);
		tf_wst.setVisible(false);
		tf_wet.setVisible(false);
		tf_salary.setVisible(false);
		tf_benefit.setVisible(false);
		tf_cname.setVisible(false);
		btnViewCompany.setVisible(false);
		btnApply.setVisible(false);
		btnNewButton_1.setVisible(false);
		lblCurrent.setVisible(false);
		lblTo.setVisible(false);
		}
	
	public void showTextFields(){
		lbl1.setVisible(true);
		lbl2.setVisible(true);
		lbl3.setVisible(true);
		lbl4.setVisible(true);
		lbl5.setVisible(true);
		lbl7.setVisible(true);
		lbl8.setVisible(true);
		lbl9.setVisible(true);
		lbl10.setVisible(true);
		lbljobid.setVisible(true);
		tf_jobid.setVisible(true);
		tf_desc.setVisible(true);
		tf_req.setVisible(true);
		tf_duty.setVisible(true);
		tf_it.setVisible(true);
		tf_et.setVisible(true);
		tf_wst.setVisible(true);
		tf_wet.setVisible(true);
		tf_salary.setVisible(true);
		tf_benefit.setVisible(true);
		tf_cname.setVisible(true);
		btnViewCompany.setVisible(true);
		btnApply.setVisible(true);
		lblCurrent.setVisible(true);
		lblTo.setVisible(true);
	}
}
